package com.pheruagor.handmanager.view {
    import com.pheruagor.handmanager.ApplicationFacade;
    import com.pheruagor.handmanager.controller.CloseStartingPopUpCommand;
    import com.pheruagor.handmanager.controller.OpenClubViewerCommand;
    import com.pheruagor.handmanager.controller.OpenStartingPopUpCommand;
    import com.pheruagor.handmanager.model.ApplicationProxy;
    import com.pheruagor.handmanager.model.business.CustomMenuItem;

    import mx.containers.VBox;
    import mx.controls.MenuBar;
    import mx.core.WindowedApplication;
    import mx.events.MenuEvent;

    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    public class ApplicationMediator extends Mediator {
        public static const NAME:String = "applicationMediator";

        //-----------------------------------------------------
        // Properties
        //-----------------------------------------------------
        private var m_applicationProxy:ApplicationProxy;
        private var m_clubSelected:Boolean;

        //-----------------------------------------------------
        // View
        //-----------------------------------------------------
        private var m_mainMenu:MenuBar;
        private var m_mainBox:VBox;

        //-----------------------------------------------------
        // Initialization
        //-----------------------------------------------------
        public function ApplicationMediator(view:WindowedApplication) {
            super(NAME, view);
            buildView();
        }

        //-----------------------------------------------------
        // IMediator Interface
        //-----------------------------------------------------
        override public function listNotificationInterests():Array {
            return [CloseStartingPopUpCommand.EXECUTED];
        }

        override public function handleNotification(notification:INotification):void {
            switch (notification.getName()) {
                case CloseStartingPopUpCommand.EXECUTED:
                    openClubViewer();
                    break;
            }
        }

        private function openClubViewer():void {
            var proxy:ApplicationProxy = ApplicationProxy(facade.retrieveProxy(ApplicationProxy.NAME));
            if (proxy.currentClub != null) {
                var mediator:ClubViewerMediator = ClubViewerMediator(facade.retrieveMediator(ClubViewerMediator.NAME));
                if (mediator != null) {
                    m_mainBox.removeChild(mediator.clubViewer);
                    facade.removeMediator(ClubViewerMediator.NAME);
                }
                sendNotification(OpenClubViewerCommand.NAME, {"container": m_mainBox, "club": proxy.currentClub});
            }
        }

        //-----------------------------------------------------
        // Access
        //-----------------------------------------------------
        public function get windowedApp():WindowedApplication {
            return WindowedApplication(viewComponent);
        }

        //-----------------------------------------------------
        // Event handling
        //-----------------------------------------------------
        private function onMainMenuClicked(event:MenuEvent):void {
            var item:CustomMenuItem = CustomMenuItem(event.item);
            switch (item.id) {
                case "exit":
                    sendNotification(ApplicationFacade.SHUTDOWN, viewComponent);
                    return;
                case "openClub":
                    sendNotification(OpenStartingPopUpCommand.NAME, viewComponent);
                    break;
            }
        }

        //-----------------------------------------------------
        // Operation
        //-----------------------------------------------------

        //-----------------------------------------------------
        // Implementation
        //-----------------------------------------------------
        private function buildView():void {
            // Full screen
            //windowedApp.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
            initializeMainContainer();
            initializeMainMenu();
            initializeStartPopUp();
        }

        private function initializeMainMenu():void {
            m_mainMenu = new MenuBar();
            m_mainMenu.percentWidth = 100;
            m_mainMenu.addEventListener(MenuEvent.ITEM_CLICK, onMainMenuClicked);
            m_applicationProxy = ApplicationProxy(facade.retrieveProxy(ApplicationProxy.NAME));
            m_mainMenu.dataProvider = m_applicationProxy.listMenuItems();
            m_mainBox.addChild(m_mainMenu);
        }

        private function initializeStartPopUp():void {
            sendNotification(OpenStartingPopUpCommand.NAME, windowedApp);
        }

        private function initializeMainContainer():void {
            m_mainBox = new VBox();
            m_mainBox.setStyle("verticalGap", 0);
            m_mainBox.percentHeight = 100;
            m_mainBox.percentWidth = 100;
            windowedApp.addChild(m_mainBox);
        }
    }
}